package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@DubboService
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public synchronized void add(List<OrderSetting> orderSettingList) {
        for (OrderSetting orderSetting : orderSettingList) {
            //判断当前日期是否已经设置预约
            OrderSetting orderSetting1 = new OrderSetting();
            orderSetting1.setOrderDate(orderSetting.getOrderDate());
            OrderSetting orderSetting2 = orderSettingMapper.selectOne(orderSetting1);
            if (orderSetting2 != null) {//当前日期已预约--》更新数据
                orderSetting2.setNumber(orderSetting.getNumber());
                orderSettingMapper.updateByPrimaryKey(orderSetting2);
            } else {//当前日期没有数据--》添加
                orderSettingMapper.insertSelective(orderSetting);
            }
        }
    }

    @Override
    public List<OrderSetting> findByDate(String startTime, String endTime) {
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andBetween("orderDate", startTime, endTime);
        List<OrderSetting> orderSettingList = orderSettingMapper.selectByExample(example);
        return orderSettingList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断当前日期是否已经设置预约
        OrderSetting orderSetting1 = new OrderSetting();
        orderSetting1.setOrderDate(orderSetting.getOrderDate());
        OrderSetting orderSetting2 = orderSettingMapper.selectOne(orderSetting1);
        if (orderSetting2 != null) {//当前日期已预约--》更新数据
            orderSetting2.setNumber(orderSetting.getNumber());
            orderSettingMapper.updateByPrimaryKey(orderSetting2);
        } else {//当前日期没有数据--》添加
            orderSettingMapper.insertSelective(orderSetting);
        }
    }
}
